/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */

package org.vineatoum.business;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;

import org.vineatoum.business.abstraction.BusinessAbstract;
import org.vineatoum.business.configuration.LieuDit;
import org.vineatoum.business.configuration.SoilType;
import org.vineatoum.view.utils.jtablecustom.TableListener;

@XmlRootElement(name = "parcelle")
@XmlAccessorType(XmlAccessType.FIELD)
public class Parcelle extends BusinessAbstract {
	public static List<Parcelle> parcelles = new ArrayList<Parcelle>();
	@XmlID
	@XmlElement
	private String name;
	@XmlElement
	private String cadastralNumber;
	@XmlIDREF
	@XmlElement
	private LieuDit lieuDit;
	// private Commune commune;
	// private String gpsLocation;
	@XmlElement
	private float surfaceArea;
	@XmlIDREF
	@XmlElement
	private SoilType soilType;

	public Parcelle() {
		parcelles.add(this);
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the cadastralNumber
	 */
	public String getCadastralNumber() {
		return cadastralNumber;
	}

	/**
	 * @param cadastralNumber
	 *            the cadastralNumber to set
	 */
	public void setCadastralNumber(String cadastralNumber) {
		this.cadastralNumber = cadastralNumber;
	}

	/**
	 * @return the lieuDit
	 */
	public LieuDit getPlaceName() {
		return lieuDit;
	}

	/**
	 * @param lieuDit
	 *            the lieuDit to set
	 */
	public void setPlaceName(LieuDit lieuDit) {
		this.lieuDit = lieuDit;
	}

	/**
	 * @return the soilType
	 */
	public SoilType getSoilType() {
		return soilType;
	}

	/**
	 * @param soilType
	 *            the soilType to set
	 */
	public void setSoilType(SoilType soilType) {
		this.soilType = soilType;
	}

	// /**
	// * @return the commune
	// */
	// public Commune getCommune() {
	// return commune;
	// }
	//
	// /**
	// * @param commune
	// * the commune to set
	// */
	// public void setCommune(Commune commune) {
	// this.commune = commune;
	// }

	// /**
	// * @return the gpsLocation
	// */
	// public String getGpsLocation() {
	// return gpsLocation;
	// }
	// /**
	// * @param gpsLocation the gpsLocation to set
	// */
	// public void setGpsLocation(String gpsLocation) {
	// this.gpsLocation = gpsLocation;
	// }
	/**
	 * @return the surfaceArer
	 */
	public float getSurfaceArea() {
		return surfaceArea;
	}

	/**
	 * @param surfaceArea
	 *            the surfaceArer to set
	 */
	public void setSurfaceArea(float surfaceArea) {
		this.surfaceArea = surfaceArea;
	}

	@Override
	public TableListener getNewInstance() {
		return new Parcelle();
	}

	@Override
	public Vector<Object> getVectorData() {
		DecimalFormat df = new DecimalFormat("########.##"); 
		
		Vector<Object> vector = new Vector<Object>();
		vector.add(name);
		vector.add(cadastralNumber);
		vector.add(lieuDit);
		// vector.add(commune);
		// vector.add(gpsLocation);
		vector.add(df.format(surfaceArea));
		vector.add(soilType);
		return vector;
	}

	@Override
	public void updateField(String field, Object value) {
		try {
			if (field.equals("Nom")) {
				this.name = (String) value;
			} else if (field.equals("Numéro Cadastral")) {
				this.cadastralNumber = (String) value;
			} else if (field.equals("Lieu Dit")) {
				this.lieuDit = (LieuDit) value;
			} else if (field.equals("Commune")) {
				// this.commune = (Commune) value;
			} else if (field.equals("GPS")) {
				// this.gpsLocation = (String) value;
			} else if (field.equals("Superficie")) {
				this.surfaceArea = (Float) value;
			} else if (field.equals("Type de sol")) {
				this.soilType = (SoilType) value;
			}
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	@Override
	public String toString() {
		return name + " - " + cadastralNumber;
	}

	@Override
	public Vector<String> getVectorColumns() {
		Vector<String> vector = new Vector<String>();
		vector.add("Nom");
		vector.add("Numéro Cadastral");
		vector.add("Lieu Dit");
		// vector.add(commune);
		// vector.add(gpsLocation);
		vector.add("Superficie");
		vector.add("Type de sol");
		return vector;
	}
}
